package com.dong.db.shard;

import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.Map;

/**
 * 生成分库分表sql语句
 */
public class GerenalShardSql {

    public static void main(String[] args) {
        String dbName = SqlPromoActivity.DATA_BASE;
        Map<String, String> sqlMap = SqlPromoActivity.getSqlMap();
        int dataBaseCount = 8;
        int preTableCount = 32;

        for (int dbIndex = 0; dbIndex < dataBaseCount; dbIndex++) {
            String useDatabase = String.format("use %s%s;", dbName, dbIndex);
            System.out.println(useDatabase);
            // 向文件中追加当前库
            String fileName = "/Users/dongliangyi/Documents/sql/" + dbName + dbIndex + ".txt";
            TxtUtils.writeTxt(fileName, true, Lists.newArrayList(useDatabase, ""));

            for (int num = 0; num < preTableCount; num++) {
                int tableIndex = dbIndex * preTableCount + num;
                sqlMap.forEach((tableName, sql) -> {
                    String tableSql = sql.replace("@table", tableName + tableIndex);
                    System.out.println(tableSql);
                    // 向文件中追加当前表
                    TxtUtils.writeTxt(fileName, true, Lists.newArrayList(tableSql, ""));
                });
            }
        }
    }
}